﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using Controllers;

namespace Web
{
    public partial class DailyList : System.Web.UI.Page
    {
        protected string datas;
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                List<int> deptIdList = Session["deptIdList"] as List<int>;
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                if(deptIdList.Count()>0)
                {
                    IList<EmpIDDept> empIdDepts;
                    empIdDepts = (from r in ctx.Employees
                                  join r2 in ctx.EmpDepPositions on r.Id equals r2.EmpId
                                  where deptIdList.Contains(r2.DeptId)
                                  orderby r2.PosId, r.Name
                                  select new EmpIDDept { Id = r.Id, Name = r.Name, PosId = 0 }).ToList();
                    ddlName.DataSource = empIdDepts;
                    ddlName.DataTextField = "Name";
                    ddlName.DataValueField = "Id";
                    ddlName.DataBind();
                    ddlName.Value = ebi.Id.ToString();
                }
                else
                {
                    ddlName.Items.Add(new ListItem(ebi.Name,ebi.Id.ToString()));
                    ddlName.Value = ebi.Id.ToString();
                }

                int year = DateTime.Today.Year;
                int month = DateTime.Today.Month;
                ddlYear.Items.Add(new ListItem((year -2).ToString(), (year-2).ToString()));
                ddlYear.Items.Add(new ListItem((year - 1).ToString(), (year - 1).ToString()));
                ddlYear.Items.Add(new ListItem(year.ToString(),year.ToString()));
                ddlYear.Value = year.ToString();
                for(int i=1;i<=12;i++)
                {
                    ddlMonth.Items.Add(new ListItem(i.ToString(),i.ToString()));
                }
                ddlMonth.Value = month.ToString();
                _getData();
            }
        }
        protected void btnSeach_Click(object sender, EventArgs e)
        {
            _getData();
        }
        private void _getData()
        {
            DateTime sDate = new DateTime(int.Parse(ddlYear.Value), int.Parse(ddlMonth.Value), 1);
            DateTime eDate = sDate.AddMonths(1);
            eDate = eDate.AddDays(-1);
            while (sDate.DayOfWeek != DayOfWeek.Sunday)
            {
                sDate = sDate.AddDays(-1);
            }
            while (eDate.DayOfWeek != DayOfWeek.Saturday)
            {
                eDate = eDate.AddDays(1);
            }
            TimeSpan timeSpan = eDate - sDate;
            int weekCnt = (timeSpan.Days + 1) / 7;
            string[] weekContents = new string[weekCnt];
            string[] weekStr = { "一", "二", "三", "四", "五", "六" };
            StringBuilder sb = new StringBuilder(@"<table class='bordertable'><tr>");
            for (int i = 0; i < weekCnt; i++)
            {
                sb.AppendFormat(@"<td colspan='7'>第{0}周</td>", weekStr[i]);
            }
            sb.Append("</tr><tr>");
            DateTime tDate = sDate;
            while (tDate <= eDate)
            {
                if (tDate == DateTime.Today)
                {
                    sb.AppendFormat("<td class='today'>{0}</td>", tDate.Day);
                }
                else
                {
                    sb.AppendFormat("<td>{0}</td>", tDate.Day);
                }
                tDate = tDate.AddDays(1);
            }

            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
            if(ddlName.Value==ebi.Id.ToString())
            {
                sb.Append("</tr><tr>");
            }
            else
            {
                sb.Append("</tr><tr style='display:none;'>");
            }
            tDate = sDate;

            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            IList<Daily> dailyList = (from r in ctx.Dailies
                                      where r.EmpId ==int.Parse(ddlName.Value)&& r.CreatDate >= sDate && r.CreatDate <= eDate
                                      select r).ToList();
            while (tDate <= eDate)
            {
                IEnumerable<Daily> dailyToday = dailyList.OrderBy(r => r.StartTime).Where(r => r.CreatDate == tDate);
                if (dailyToday.Count() > 0)
                {
                    sb.AppendFormat("<td><img title='编辑' src='Images/tab_edit.png' onclick=\"edit('{0}');\"/></td>", tDate.ToString("yyyy-MM-dd"));
                    int weekSn = ((tDate - sDate).Days + 1) / 7;
                    int weekMod = ((tDate - sDate).Days + 1) % 7;
                    if (weekMod == 0)
                    {
                        weekSn--;
                    }
                    weekContents[weekSn] += string.Format("<font color='#cc0000'>{0}号:</font><br />", tDate.Day);
                    foreach (Daily d in dailyToday)
                    {
                        weekContents[weekSn] += string.Format("<div>{0}</div><br />", Common.FormatTextArea(d.Record));
                    }
                }
                else
                {
                    sb.AppendFormat("<td><img title='新增' src='Images/receipts--plus.png' onclick=\"edit('{0}');\"/></td>", tDate.ToString("yyyy-MM-dd"));
                }
                tDate = tDate.AddDays(1);
            }
            sb.Append("</tr><tr class='fLeft'>");
            for (int i = 0; i < weekCnt; i++)
            {
                sb.AppendFormat(@"<td colspan='7'>{0}</td>", weekContents[i] ?? "无");
            }
            sb.Append("</tr></table>");
            datas = sb.ToString();
        }
    }
}